/**
 * Azpiration Common Javascript Function (ACJF)
 *
 * @author      harry s. kartono <hskartono@yahoo.co.id>
 * @version     0.1
 * @copyright   Azpiration 2006
 * @filename    function.js
 */

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
 
var _BASE_PATH;

function setBasePath(url) {
	//_BASE_PATH = '<%= Url.Content("~/")%>';
	_BASE_PATH = url;
	// if last string = "/", remove it
	if (_BASE_PATH.charAt(_BASE_PATH.length-1) == "/") {
		_BASE_PATH = _BASE_PATH.substring(0, _BASE_PATH.length-1);
	}
}



/**
 * getting date different value
 */
function dateDiff(strDate1,strDate2) {
	return (((Date.parse(strDate2))-(Date.parse(strDate1)))/(24*60*60*1000)).toFixed(0);
}
 
/**
 * get date different value in UK format
 */
function dateDiffUK(strDate1, strDate2) {
	date1 = fromUKFormat(strDate1);
	date2 = fromUKFormat(strDate2);
	return dateDiff(date1, date2);
}
 
/**
 * convert from UK format to US format
 */
function fromUKFormat(sUK) {
	var A = sUK.split(/[\\\/]/);
	A = [A[1],A[0],A[2]];
	return new Date(Date.parse(A.join('/')));
}

/**
 * Leading Zero function
 */
function LZ(x) {return(x<0||x>9?"":"0")+x}

/**
 * function for formatting date entry (date object)
 *
 * y	default year format
 * yy	the 2 digit year format
 * yyyy	the 4 digit year format
 * M	default month format (1 to 12)
 * MM	the 2 digit month format (1 to 12)
 * MMM	the 3 word month name (jan to dec)
 * MMMM	complete month name (january to december)
 * d	default date format
 * dd	the 2 digit date format (1 to 31)
 * E	the 3 word day name (Sun to Sat)
 * EE	complete day name (Sunday to Saturday)
 * h	
 * hh	
 * m	
 * mm	
 * K	
 * KK	
 * k	
 * kk	
 */
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["NNN"]=MONTH_NAMES[M+11];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["E"]=DAY_NAMES[E+7];
	value["EE"]=DAY_NAMES[E];
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
	}
	return result;
}

/**
 * method for calculating date value after few ammount of day
 */
function getDateAfter(strStartDate, day) {
	var str_date = strStartDate.split("/");
	var start_date = new Date(str_date[2], (str_date[1]-1), str_date[0]);
	var date_after = new Date(start_date.getFullYear(), start_date.getMonth(), start_date.getDate() + day);
	return date_after;
}

/**
 * method for checking numeric value
 */
function isNumeric(strString) {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   if (isNaN(strString)) return false;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function rounding(num, limit) {
    result = (isNaN(num))?0:num;
    round_limit = Math.pow(10, limit);
    result = Math.round(num*round_limit)/round_limit;
    return result;
}